using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Globalization;
using ns17;
using ns20;

namespace Newtonsoft.Json.Converters
{
	public class BinaryConverter : JsonConverter
	{
		private const string string_0 = "System.Data.Linq.Binary";

		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			if (value == null)
			{
				writer.WriteNull();
				return;
			}
			byte[] value2 = method_0(value);
			writer.WriteValue(value2);
		}

		private byte[] method_0(object object_0)
		{
			if (object_0.GetType().smethod_14("System.Data.Linq.Binary"))
			{
				Interface8 @interface = Class164.smethod_5<Interface8>(object_0);
				return @interface.imethod_0();
			}
			if (!(object_0 is SqlBinary))
			{
				throw new JsonSerializationException("Unexpected value type when writing binary: {0}".smethod_0(CultureInfo.InvariantCulture, object_0.GetType()));
			}
			return ((SqlBinary)object_0).Value;
		}

		public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
		{
			Type type = (Class195.smethod_10(objectType) ? Nullable.GetUnderlyingType(objectType) : objectType);
			if (reader.JsonToken_0 == JsonToken.Null)
			{
				if (!Class195.smethod_9(objectType))
				{
					throw JsonSerializationException.smethod_1(reader, "Cannot convert null value to {0}.".smethod_0(CultureInfo.InvariantCulture, objectType));
				}
				return null;
			}
			byte[] array;
			if (reader.JsonToken_0 == JsonToken.StartArray)
			{
				array = method_1(reader);
			}
			else
			{
				if (reader.JsonToken_0 != JsonToken.String)
				{
					throw JsonSerializationException.smethod_1(reader, "Unexpected token parsing binary. Expected String or StartArray, got {0}.".smethod_0(CultureInfo.InvariantCulture, reader.JsonToken_0));
				}
				string s = reader.Object_0.ToString();
				array = Convert.FromBase64String(s);
			}
			if (type.smethod_14("System.Data.Linq.Binary"))
			{
				return Activator.CreateInstance(type, array);
			}
			if (!(type == typeof(SqlBinary)))
			{
				throw JsonSerializationException.smethod_1(reader, "Unexpected object type when writing binary: {0}".smethod_0(CultureInfo.InvariantCulture, objectType));
			}
			return new SqlBinary(array);
		}

		private byte[] method_1(JsonReader jsonReader_0)
		{
			List<byte> list = new List<byte>();
			while (jsonReader_0.Read())
			{
				switch (jsonReader_0.JsonToken_0)
				{
				case JsonToken.Integer:
					list.Add(Convert.ToByte(jsonReader_0.Object_0, CultureInfo.InvariantCulture));
					break;
				case JsonToken.Comment:
					break;
				default:
					throw JsonSerializationException.smethod_1(jsonReader_0, "Unexpected token when reading bytes: {0}".smethod_0(CultureInfo.InvariantCulture, jsonReader_0.JsonToken_0));
				case JsonToken.EndArray:
					return list.ToArray();
				}
			}
			throw JsonSerializationException.smethod_1(jsonReader_0, "Unexpected end when reading bytes.");
		}

		public override bool CanConvert(Type objectType)
		{
			if (objectType.smethod_14("System.Data.Linq.Binary"))
			{
				return true;
			}
			if (!(objectType == typeof(SqlBinary)) && !(objectType == typeof(SqlBinary?)))
			{
				return false;
			}
			return true;
		}
	}
}
